import axios from 'axios'

const request = axios.create({
  // api 的 base_url
  baseURL: import.meta.env.MODE === 'development' ? '' :  import.meta.env.VITE_API_DOMAIN, 
  timeout: 60000
})

request.interceptors.request.use(config => {
  if (localStorage.getItem('Authorization') !== null) {
    config.headers.Authorization = localStorage.getItem('Authorization')
  } else {
  }
  return config
}, error => {
  return Promise.reject(error)
})

request.interceptors.response.use((res) => {
  // console.log("--------------",res);
  //无权限，直接跳转登录页
  if(res.status === 401){
    window.localStorage.removeItem('Authorization');
    window.location.href = '/login';
  }
  if (res.status === 200) {
    if (res.data.code === 10000) {
      window.localStorage.removeItem('Authorization');
      window.location.href = '/login';
      // MessageBox.alert(res.data.msg, '登陆失效', {
      //   confirmButtonText: '跳转登录页',
      //   callback: action => {
      //     window.location.href = '/'
      //   }
      // })
    } else {
      return Promise.resolve(res.data)
    }
  } else {
    return Promise.reject(res.msg)
  }
})

export default request
